package zhu.common.data;

import java.util.List;

//用于封装分页信息
public class PageBean {

	private List list; // 要返回的某一页的记录列表
	private Long allRow; // 总记录数
	private int totalPage; // 总页数
	private int currentPage; // 当前页
	private int pageSize; // 每页记录数
	private boolean isFirstPage; // 是否为第一页
	private boolean isLastPage; // 是否为最后一页
	private boolean hasPreviousPage; // 是否有前一页
	private boolean hasNextPage; // 是否有下一页

	// 初始化分页信息
	public void init() {
		this.isFirstPage = isFirstPage();
		this.isLastPage = isLastPage();
		this.hasPreviousPage = isHasPreviousPage();
		this.hasNextPage = isHasNextPage();
	}

	// 计算总页数
	public int countTotalPage(final int pageSize, final Long allRow) {
		int totPage = (int) (allRow % pageSize == 0 ? allRow / pageSize
				: allRow / pageSize + 1);
		this.setTotalPage(totPage);
		return totPage;
	}

	// 计算当前页开始记录
	public int countOffset(final int pageSize, final int currentPage) {
		return pageSize * (currentPage - 1);
	}

	// 计算当前页
	public int countCurrentPage(int page) {
		int curPage;
		if (page <= 0) {
			curPage = 1;
		} else {
			if (page > totalPage) {
				curPage = totalPage;
			} else {
				curPage = page;
			}
		}
		this.setCurrentPage(curPage);
		return curPage;
	}

	public List getList() {
		return list;
	}

	public void setList(List list) {
		this.list = list;
	}

	public Long getAllRow() {
		return allRow;
	}

	public void setAllRow(Long allRow) {
		this.allRow = allRow;
	}

	public int getTotalPage() {
		return totalPage;
	}

	public void setTotalPage(int totalPage) {
		this.totalPage = totalPage;
	}

	public int getCurrentPage() {
		return currentPage;
	}

	public void setCurrentPage(int currentPage) {
		this.currentPage = currentPage;
	}

	public int getPageSize() {
		return pageSize;
	}

	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}

	public void setFirstPage(boolean isFirstPage) {
		this.isFirstPage = isFirstPage;
	}

	public void setLastPage(boolean isLastPage) {
		this.isLastPage = isLastPage;
	}

	public void setHasPreviousPage(boolean hasPreviousPage) {
		this.hasPreviousPage = hasPreviousPage;
	}

	public void setHasNextPage(boolean hasNextPage) {
		this.hasNextPage = hasNextPage;
	}

	public boolean isFirstPage() {
		return currentPage == 1; // 如是当前页是第1页
	}

	public boolean isLastPage() {
		return currentPage == totalPage; // 如果当前页是最后一页
	}

	public boolean isHasPreviousPage() {
		return currentPage != 1; // 只要当前页不是第1页
	}

	public boolean isHasNextPage() {
		return currentPage != totalPage; // 只要当前页不是最后1页
	}
}
